package com.dikang.yygh.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.dikang.yygh.common.exception.YyghException;
import com.dikang.yygh.common.handler.HttpRequestHelper;
import com.dikang.yygh.common.result.Result;
import com.dikang.yygh.common.result.ResultCodeEnum;
import com.dikang.yygh.model.hosp.Department;
import com.dikang.yygh.model.hosp.Schedule;
import com.dikang.yygh.service.DepartmentService;
import com.dikang.yygh.service.HospitalService;
import com.dikang.yygh.service.HospitalSetService;
import com.dikang.yygh.service.ScheduleService;
import com.dikang.yygh.vo.hosp.DepartmentQueryVo;
import com.dikang.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 供其他调用的
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramrMap = HttpRequestHelper.switchMap(requestMap);
        // 根据传递过来的医院名称和加密的sign和数据库中的数据相对比
        String hoscode = (String) paramrMap.get("hoscode");
        String sign = (String) paramrMap.get("sign");
        /*String signKey = hospitalSetService.getSignKey(hoscode);
        if (signKey !=null &&signKey!=""){
            if (!sign.equals(signKey)){

                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
            hospitalService.save(paramrMap);
        }*/
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        // 传输过来的数据“+”转换成了“ ”因此要转换过来
        String logoData = (String) paramrMap.get("logoData");
        if (StringUtils.isEmpty(logoData)) {
            throw new YyghException(ResultCodeEnum.DATA_ERROR);
        }

        logoData = logoData.replaceAll(" ", "+");
        paramrMap.put("logoData", logoData);
        // 根据传过来的hoscode获取他的sign和传过来的作对比
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        hospitalService.save(paramrMap);
        return Result.ok();
    }

    /**
     * 显示医院信息
     *
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramrMap = HttpRequestHelper.switchMap(requestMap);
        // 根据传递过来的医院名称和加密的sign和数据库中的数据相对比
        String hoscode = (String) paramrMap.get("hoscode");
        String sign = (String) paramrMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        // 查询出hospital数据
        return Result.ok(hospitalService.getHospitalByHosCode(hoscode));
    }

    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramrMap = HttpRequestHelper.switchMap(requestMap);
        // 根据传递过来的医院名称和加密的sign和数据库中的数据相对比
        String hoscode = (String) paramrMap.get("hoscode");
        String sign = (String) paramrMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        departmentService.saveDepartment(paramrMap);


        return Result.ok();
    }

    // 查询科室列表
    @PostMapping("department/list")
    public Result getDepartmentList(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String pageStr = (String) paramMap.get("page");
        String limitStr = (String) paramMap.get("limit");
        //非必填
        String depcode = (String) paramMap.get("depcode");
        Integer page = !StringUtils.isEmpty(pageStr) ? Integer.parseInt(pageStr) : 1;
        Integer limit = !StringUtils.isEmpty(limitStr) ? Integer.parseInt(limitStr) : 10;
        // 根据传递过来的医院名称和加密的sign和数据库中的数据相对比
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);

        Page<Department> departmentPage = departmentService.findPageDepartment(page, limit, departmentQueryVo);

        return Result.ok(departmentPage);
    }

    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //非必填
        String depcode = (String) paramMap.get("depcode");
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        departmentService.remove(depcode, hoscode);

        return Result.ok();
    }

    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //非必填
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        scheduleService.save(paramMap);

        return Result.ok();
    }

    @PostMapping("schedule/list")
    public Result getScheduletList(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //非必填
        String hoscode = (String) paramMap.get("hoscode");
        String pageStr = (String) paramMap.get("page");
        String limitStr = (String) paramMap.get("limit");
        //非必填
        String depcode = (String) paramMap.get("depcode");
        String doccode = (String) paramMap.get("doccode");
        Integer page = !StringUtils.isEmpty(pageStr) ? Integer.parseInt(pageStr) : 1;
        Integer limit = !StringUtils.isEmpty(limitStr) ? Integer.parseInt(limitStr) : 10;
        String sign = (String) paramMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setDepcode(depcode);
        scheduleQueryVo.setDoccode(doccode);
        scheduleQueryVo.setHoscode(hoscode);
        Page pageSchedule = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageSchedule);
    }
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //非必填
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        // 根据hoscode查询出sign相比较秘钥是否一致
        if (StringUtils.isEmpty(hoscode)) {
            new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (!StringUtils.isEmpty(signKey)) {
            if (!sign.equals(signKey)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        }
        scheduleService.remove(hoscode,hosScheduleId);

        return Result.ok();
    }

}
